#pragma once

#ifdef __QT5_TARGET

#include <QtCore/QAbstractTableModel>
#include <QtWidgets/QMessageBox>

#else

#include <QMessageBox>
#include <QAbstractTableModel>

#endif

#include "korlapadmin_model.h"

enum Shift {MORNING,SIESTA,AFTERNOON,NIGHT};

class ScheduleTableModel :
	public QAbstractTableModel
{
	Q_OBJECT
	public slots:
		QBrush brushItem(Schedule* s) const;
		QBrush brushHovered(Schedule* s)const;
		QBrush brushGreen() const;
		QBrush brushRed()const;
		QBrush brushYellow()const;
		void hoverCell(const QModelIndex& index);
		void unHoverCell();
		void refreshTable();
		
public:
	
	//ScheduleTableModel(void);
	void addSchedule(const QString& note);
	Qt::ItemFlags flags ( const QModelIndex & index ) const;
	korlapAdmin_model* getModel()const{return this->model;}
	ScheduleTableModel(korlapAdmin_model* model, QLocale* locale);
	~ScheduleTableModel(void);
	int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
	int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
	QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;
	QTime getMorningStart();
	QTime getMorningEnd();
	QTime getNightStart();
	QTime getNightEnd();
	QBrush getHoveredFreeBrush()const{return *(this->hoveredFreeBrush);}
	QBrush getFreeBrush()const{return *(this->freeHourBrush);}
	void setMorningStart(const QTime& time);
	void setMorningEnd(const QTime & time);
	void setNightStart(const QTime & time);
	void setNightEnd(const QTime & time);
	void setModel(korlapAdmin_model* model);
	QPair<QDateTime*,QDateTime*>* getWeekRange();
	QList<QPair<QPair<int,int>,int>> *getRowsToSpan();
	QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
	void setPatient(const Patient* p);
	const Patient* getPatient()const;
	QModelIndex* getLastHovered();
	void setLastHovered(QModelIndex* index);
	void setHoveredRow(int row);
	int getHoveredRow()const;
	void setEditing(bool e);
	bool isEditing()const{return editing;}
	void setSelectedCells(const QPair<QPersistentModelIndex,QPersistentModelIndex>& lst);
	QPair<QPersistentModelIndex,QPersistentModelIndex> getSelectedCells()const{return selectedCells;}
signals:
	void rowHovered();
private:
	bool editing;
	QPair<int,int> lastHovered;
	QPair<int,int> cellToHover;
	QPair<QPersistentModelIndex,QPersistentModelIndex> selectedCells;
	QString currentHoveredLabel;
	QTime beginningOfMorningShift;
	QTime endOfMorningShift;
	QTime beginningOfNightShift;
	QTime endOfNightShift;
	QTime siestaTime;
	int hoveredRow;
	QList<QPair<QPair<int,int>,int>> *extendList;
	bool siesta;
	bool night;
	const QMessageBox* messageBox;
	Shift currentShift;
	korlapAdmin_model* model;
	ScheduleTableModel * nextWeek;
	ScheduleTableModel* prevWeek;
	QLocale* locale;
	QPair<QDateTime*,QDateTime*>* weekRange;
	const Patient *currentSelectedPatient;

	void initializeCurrentHourBrush();
	void initializeCurrentHourReservedBrush();
	void initializeFreeHourBrush();
	void initializeReservedHourBrush();
	void initializeHoveredFreeBrush();

	QBrush* currentHourFreeBrush;
	QBrush* currentHourReservedBrush;
	QBrush* freeHourBrush;
	QBrush* reservedHourBrush;
	QBrush* hoveredFreeBrush;
	QColor currentDayBorder;
	QColor dayBorder;
};
